/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package library.Configuration;

import java.io.InputStream;
import java.net.URL;

import org.apache.log4j.*;

/**
 * A configuration file can be referenced as an URL (applet mode) or as a file
 * reference (application mode)
 * 
 * This class embeds all specificities related to URL-based configuration file
 * 
 * @author JNC
 */
public class ConfigFromURL extends Config {
    /**
     * URL of the configuration file
     * The configuration file is opened with the first call to read a config value
     * As this time is not predictable, the name of the configuration file
     * is recorded as soon as it is known
     */
    private static URL _configURL = null;
    /**
     * The log4j logger
     */
    private static final Logger _logger = Logger.getLogger(ConfigFromURL.class);    
    
    /**
     * Sets the URL that references the config file
     * @param myURL
     * The URL that represents the config file
     */
    protected void setConfigFilename(URL myURL) {
        _logger.debug("Config file is accessed via URL " + myURL);
       _configURL = myURL; 
    }
    
    /**
     * Opens the URL to get the config file
     * @return
     * A reference to the config file
     */
    protected InputStream openConfigFile() {
        if (_configURL != null) {
            try {
                _logger.info("Config URL = " + _configURL);
                return _configURL.openStream();
            }
            catch(java.io.IOException exception) {
                throw new FatalConfigurationException("IOException caught while opening url : " + _configURL + " (" + exception + ")");
            }
        } else {
            throw new FatalConfigurationException("Attempt to read a configuration value while URL is null");
        }
    }
}
